﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmTrangThaiGiaDinh : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmTrangThaiGdEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmTrangThaiGiaDinhBusiness dmTrangThaiGiaDinhBusiness = new DmTrangThaiGiaDinhBusiness();
        DmTrangThaiGiaDinhEntity dmTrangThaiGiaDinhEntity = null;

        public frmCapNhatDmTrangThaiGiaDinh(int state)
        {
            InitializeComponent();
            this.State = state;
        }

        public frmCapNhatDmTrangThaiGiaDinh(int state, DmTrangThaiGiaDinhEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmTrangThaiGiaDinhEntity = obj;

            txtMaTrangThai.Text = dmTrangThaiGiaDinhEntity.MaTrangThai;
            txtTenTrangThai.Text = dmTrangThaiGiaDinhEntity.TenTrangThai;
            txtMoTa.Text = dmTrangThaiGiaDinhEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmTrangThaiGiaDinhEntity.TrangThaiSuDung;
            txtMaTrangThai.Enabled = false;
            txtTenTrangThai.Focus();
        }

        private void frmcapnhatdmtrangthaigiadinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaTrangThai, "Mã trạng thái gia đình") == false) return;
            
            dmTrangThaiGiaDinhEntity = new DmTrangThaiGiaDinhEntity()
            {
                MaTrangThai = txtMaTrangThai.Text,
                TenTrangThai = txtTenTrangThai.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };
           
            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmTrangThaiGiaDinhBusiness.GetDmTrangThaiGiaDinh(txtMaTrangThai.Text.Trim()) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaTrangThai, "Mã trạng thái gia đình") == false) return;
                }
                dmTrangThaiGiaDinhBusiness.InsertDmTrangThaiGiaDinh(dmTrangThaiGiaDinhEntity);
                Validation.ClearControl(this);
                txtMaTrangThai.Focus();
            }
            else
            {
                dmTrangThaiGiaDinhBusiness.EditDmTrangThaiGiaDinh(dmTrangThaiGiaDinhEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            if (dmTrangThaiGdEvent != null)
                dmTrangThaiGdEvent(sender, e);
        }

    }
}